package atguigu.huoquToken;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class TokenTest {
	public static final String TOKENURL = "http://127.0.0.1:8001/LIMS-Server/MPLM_Service_Gateway/TokenGenerator/getToken";
	public static final String APIURL = "http://127.0.0.1:8001/LIMS-Server/MPLM_Service_Gateway/AppTestProcessController/startTest";

	public static void main(String[] args) {

		String userName = "chenbo16";
		String func = "TestProcessController_startTest";

		doPost(APIURL, userName, func);

	}

	public static String getToken(String url, String userName, String func) {
		String res = null;
		StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(
				Charset.forName("utf-8"));
		RestTemplate restTemplate = new RestTemplate(Collections.singletonList(stringHttpMessageConverter));

		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
		headers.add("__TokenAuthorization_UserName_", userName);
		headers.add("__TokenAuthorization_Function_", func);// 请求模块名称,写死了
		HttpEntity<MultiValueMap<String, String>> request = new HttpEntity(headers);
		ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
		int responseCode = response.getStatusCodeValue();// 从状态行中获取状态码
		if (responseCode == 200) {
			String body = response.getBody();
			JSONObject jsonObject = JSON.parseObject(body);
			res = jsonObject.getString("token");
			System.out.println(res);
		}
		return res;
	}

	public static RestTemplate getInstance(String charset) {
		RestTemplate restTemplate = new RestTemplate();
		List<HttpMessageConverter<?>> list = restTemplate.getMessageConverters();
		for (HttpMessageConverter<?> httpMessageConverter : list) {
			if (httpMessageConverter instanceof StringHttpMessageConverter) {
				((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(Charset.forName(charset));
				break;
			}
		}
		return restTemplate;
	}

	public static void doPost(String apiUrl, String userName, String func) {

		String token = getToken(TOKENURL, userName, func);
		String res = null;

		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
		HttpPost httpPost = new HttpPost(apiUrl);
		httpPost.addHeader("__TokenAuthorization_UserName_", "chenbo16");
		httpPost.addHeader("__TokenAuthorization_Function_", "TestProcessController_startTest");
		httpPost.addHeader("__TokenAuthorization_UID_", token);
		HttpResponse httpResponse = null;
		org.apache.http.HttpEntity entity = null;
		String result = "";
		try {
			httpResponse = closeableHttpClient.execute(httpPost);
			entity = httpResponse.getEntity();
			if (entity != null) {
				byte[] json = EntityUtils.toByteArray(entity);
				result = new String(json, "UTF-8");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				EntityUtils.consume(entity);
			} catch (IOException e) {
				e.printStackTrace();
			}

			// 关闭连接
			try {
				closeableHttpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		res = result;
		System.out.println(res);
	}

}
